package wangjg.mda.tra;

import java.io.File;

/**
 * 代码字符工具
 * @author wangjg
 *
 */
public class CodeStringUtil {
	
	public String getPropertyName(String methodName){
		String name = methodName;
		if((name.startsWith("get") || name.startsWith("set")) && name.length()>3){
			name = Character.toLowerCase(name.charAt(3))+name.substring(4);
		}else if(name.startsWith("is") && name.length()>2){
			name = Character.toLowerCase(name.charAt(2))+name.substring(3);
		}
		return name;
	}

	public String getGetMethod(String name){
		StringBuilder sb = new StringBuilder();
		sb.append("get");
		sb.append(name);
		sb.setCharAt(3, Character.toUpperCase(name.charAt(0)));
		return sb.toString();
	}

	public String getSetMethod(String name){
		StringBuilder sb = new StringBuilder();
		sb.append("set");
		sb.append(name);
		sb.setCharAt(3, Character.toUpperCase(name.charAt(0)));
		return sb.toString();
	}
	
	public String getSimpleType(final String type){
		if(type==null){
			return "";
		}
		int pos =  type.lastIndexOf('.');
		if(pos==-1){
			return type;
		}
		return type.substring(pos+1);
	}
	
	public String getVariable(String str){
		if(str==null||str.length()==0||Character.isLowerCase(str.charAt(0))){
			return str;
		}
		return Character.toLowerCase(str.charAt(0))+str.substring(1);
	}
	
	public String getSuperPackage2(String packageName, int level){
		if(packageName==null){
			return null;
		}
		String[] a = packageName.split("\\.");
		if(a.length<level){
			return null;
		}
		StringBuilder sb = new StringBuilder();
		for(int i=0;i<level;i++){
			sb.append(a[i]);
			sb.append('.');
		}
		return sb.substring(0,sb.length()-1);
	}
	
	
	public String getSuperPackage(String packageName, int level){
		if(packageName==null){
			return null;
		}
		String[] a = packageName.split("\\.");
		
		int pLevel = a.length - level;
		
		if(pLevel<0 || pLevel>a.length){
			return null;
		}
		StringBuilder sb = new StringBuilder();
		for(int i=0;i<pLevel;i++){
			sb.append(a[i]);
			sb.append('.');
		}
		if(sb.length()==0){
			return sb.toString();
		}else{
			return sb.substring(0,sb.length()-1);
		}
	}
	
	public String getSampleName(String fullname){
		if(fullname==null){
			return null;
		}
		if(fullname.length()==0){
			return fullname;
		}
		int pos = fullname.lastIndexOf('.');
		if(pos==-1){
			return fullname;
		}
		return fullname.substring(pos+1);
	}
	
	public String getHumpWord(String name){
		if(name==null){
			return null;
		}
		if(name.length()==0){
			return name;
		}
		return Character.toUpperCase(name.charAt(0))+name.substring(1);
	}
	
	int __________;
	
	public String getPackagePath(String packageName){
		char FS = File.separatorChar;
		String path = packageName.replace('.', FS);
		return path;
	}
	
	public String getPackageUrl(String packageName, Integer level){
		StringBuilder sb = new StringBuilder();
		String[] arr = packageName.split("\\.");
		if(level==null){
			for(int i=arr.length-1;i>=2;i--){
				sb.append('/');
				sb.append(arr[i]);
			}
		}else{
			for(int i=arr.length-1;i>=(arr.length-level);i--){
				sb.append('/');
				sb.append(arr[i]);
			}
		}
		return sb.toString();
	}
	
	public static void main(String args[]){
		Object o = new CodeStringUtil().getSuperPackage("aa.bb.cc",-1);
		System.out.println(o);
	}
}
